
let form = document.querySelector('form');
let usna = document.querySelector('input[name="username"]');
let pass = document.querySelector('input[name="password"]');
let login = document.querySelector('input[value="登录"]');
let zhu = document.querySelector('input[value="注册"]');
let p1 = document.querySelector('.user');
let p2 = document.querySelector('.pswd');

function focus() {
    usna.onfocus = function () {
        usna.placeholder = '';
        p1.innerHTML = '';
    }
    usna.onblur = function () {
        user();
    }
}

function blur() {
    pass.onfocus = function () {
        pass.placeholder = '';
        p2.innerHTML = '';
    }
    pass.onblur = function () {
        pswd();
    }
}
focus();
blur();

function user() {
    if (usna.value === '') {
        p1.innerHTML = '用户名不能为空！';
        return false;

    } else {
        p1.innerHTML = '';
        return true;
    }
}

function pswd() {
    if (pass.value === '') {
        p2.innerHTML = '密码不能为空！';
        return false
    } else {
        p2.innerHTML = '';
        return true;
    }
}

function zhuce() {
    zhu.onclick = function () {
        let usnava = usna.value;
        let pasdva = pass.value;
        let reg = /^[A-Z][0-9a-zA-Z]{6,12}$/;//首字母大写，6-12位
        console.log(pasdva);

        if (reg.test(pasdva)) {
            alert("注册成功")
            let admin = {
                name: usnava,
                password: pasdva
            }
            localStorage.setItem('Admin', JSON.stringify(admin));
            // location.href = "../html/car.html";

        } else {
            alert('密码格式不对')
        }
    }
}
zhuce()
login.onclick = function (e) {
    let admin = localStorage.getItem('Admin');
    if (!admin) {
        return alert("用户名或密码不存在");
    }
    let adminer = JSON.parse(admin)
    console.log(adminer);
    e.preventDefault();
    var isUserName = user();
    var isPassword = pswd();
    if (isUserName && isPassword) {
        if (usna.value === adminer.name && pass.value === adminer.password) {
            alert("登录成功");
            location.href = "../html/car.html";
        } else {
            p1.innerHTML = '';
            p2.innerHTML = '';
            console.log(p1.innerHTML, p2.innerHTML);
            alert('账号或密码错误');
        }
    }
}

